package com.abbas.smartCity.ui.kt

import com.abbas.smartCity.App
import com.abbas.smartCity.base.BaseBindingActivity
import com.abbas.smartCity.bean.LoginBean
import com.abbas.smartCity.bean.NetBean
import com.abbas.smartCity.databinding.ActivityLoginBinding
import com.abbas.smartCity.retroft.ICallback
import com.abbas.smartCity.retroft.ICallback2
import com.abbas.smartCity.ui.HomeActivity
import com.abbas.smartCity.util.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class KLoginActivity : BaseBindingActivity<ActivityLoginBinding>(), NetScope {

    override fun setCustomTitle(): String = "登录"

    override fun initData() {

    }

    override fun initEvent() {
        binding.loginBt.setOnClickListener {
            val dialog = DialogManager.getInstance().dialog.also { it.show() }
            retrofit {
                login(
                    mapOf(
                        "username" to binding.accEt.text(),
                        "password" to binding.passEt.text()
                    ).toRequestBody()
                ).run {
                    onSuccess {
                        App.getSharedPreferences()
                            .edit()
                            .putString("token", it.token)
                            .apply()
                        userInfo.run {
                            onSuccess {
                                App.setUser(it.user)
                                dialog.dismiss()
                                startActivity(HomeActivity::class.java)
                            }
                            onFailure {
                                dialog.dismiss()
                            }
                        }
                    }
                    onFailure {
                        showToast(it)
                        dialog.dismiss()
                    }
                }
            }
        }
    }
}